<?php

/**
 *  前台用户控制器
 * @author xiaojy  xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class UserController extends Controller
{

    public function actionDetail()
    {
        $sid = Yii::app()->request->getQuery('sid');//
        if (Yii::app()->request->isPostRequest) {
            $user = Yii::app()->request->getPost('user');
            $re = User::model()->updateUser($user);
            if ($re['errno'] == 0) {
                $this->showSuccess('修改成功', $this->createUrl('/user/list'));
            } else {
                $this->showError('修改失败', $this->referer);
            }
        }
        $result = User::model()->getBySid($sid);
        $this->render('detail', array('user' => $result['result']));
    }

    public function actionLogin()
    {
        $this->layout = '';
        $checkCode = $this->createAction('captcha')->getVerifyCode();

        if (Yii::app()->request->isPostRequest) {
            $mobile = Yii::app()->request->getPost('mobile');
            $password = Yii::app()->request->getPost('password');
            $captcha = Yii::app()->request->getPost('captcha');

            if (empty($captcha) || $captcha != $checkCode) {
                $this->showMessage('验证码错误', $this->createUrl('/user/login'));
                return;
            }
            $ip = Yii::app()->request->getUserHostAddress();


            $result = User::model()->login($mobile, $password);

            if ($result['errno'] == 0) {
                $arr = array();
                $arr['ip'] = $ip;
                $arr['sid'] = Yii::app()->user->getId();
                $arr['check_method'] = 1;
                $login = LoginLog::model()->add($arr);

                $this->showSuccess('登录成功', $this->createUrl('/site/index'));
                return;
            } else {
                $this->showMessage($result['errmsg'], $this->createUrl('/user/login'));
                return;
            }
        }
        $this->render('login');
    }

    public function actionSignup()
    {
        $this->layout = '';
        $user = array();
        $user['mobile'] = '';
        $user['password'] = '';
        $user['captcha'] = '';
        if (Yii::app()->request->isPostRequest) {
            $user['mobile'] = Yii::app()->request->getPost('mobile');
            $user['password'] = Yii::app()->request->getPost('password');
            $user['captcha'] = Yii::app()->request->getPost('captcha');
            if (!empty($user['mobile']) && !empty($user['password'])) {
                if (empty($user['captcha'])) {
                    $this->showMessage('验证码错误', $this->referer);
                    return;
                }
                if (true) {
                    $user_info = User::model()->signUp($user);
                    if ($user_info['errno'] == 0) {

                        $result = User::model()->login($user['mobile'], $user['password']);
                        $this->showSuccess('注册成功', $this->createUrl('/user/info'));
                        return;
                    } else {
                        $this->showMessage('失败' . $user_info['errmsg']);
                    }
                } else {
                    $this->showMessage('验证码错误');
                }
            } else {
                $this->showMessage('请填写完整信息', $this->createUrl('/user/signup'));
            }
        }
        $this->render('signup', compact('user'));
    }

    public function actionLogout()
    {
        //删除session信息
        Yii::app()->user->logout(false);
        $this->redirect($this->createUrl('/site/index'));
    }

    //ajax上传
    public function actionUpload()
    {
        $bhv = new FilesComponent;
        $upfile = $bhv->upload('file');
        if (!$upfile) {
            $result['error'] = $bhv->error;
        } else {
            $result = $upfile;
        }
        echo CJSON::encode($result);
    }

    public function actionUploadavatar()
    {
        $bhv = new FilesComponent;
        $upfile = $bhv->upload('file');
        if (!$upfile) {
            $result['error'] = $bhv->error;
        } else {

            $hash = $upfile['hash'];
            $path = $bhv->getFilePath($hash);
            $size = getimagesize($path);
            $user['sid'] = $this->user_id;
            $user['avatar'] = $hash;
            $user_info = User::model()->updateUser($user);
            if ($user_info['errno'] == 0) {
                $result['errno'] = 0;
                $result['result'] = WxUtil::getGoodsImgUrl($hash);
                $result['size'] = $size;
            } else {
                $result['errno'] = 1;
                $result['errmsg'] = '更新失败';
            }
        }
        echo CJSON::encode($result);
    }

    //忘记密码
    public function actionResetpassword()
    {
        if (Yii::app()->request->isPostRequest) {
            $user['mobile'] = Yii::app()->request->getPost('mobile');
            $user['password'] = Yii::app()->request->getPost('password');
            $user['captcha'] = Yii::app()->request->getPost('captcha');
            if (empty($user['mobile']) || empty($user['password']) || empty('captcha')) {
                $this->showMessage('请填写完整信息', $this->createUrl('/user/resetpassword'));
            } else {
                $re = User::model()->retrievePassword($user);
                if ($re['errno'] == 0) {
                    $this->showSuccess('修改成功', $this->createUrl('/user/login'));
                } else {
                    $this->showMessage('失败' . $re['errmsg'], $this->createUrl('/user/resetpassword'));
                }
            }
        }
        $this->render('forgetpsw');
    }

    //修改密码
    public function actionResetpwd()
    {
        if (Yii::app()->request->isPostRequest) {
            $user['mobile'] = Yii::app()->request->getPost('mobile');
            $user['password'] = Yii::app()->request->getPost('password');
            $user['captcha'] = Yii::app()->request->getPost('captcha');
            if (empty($user['mobile']) || empty($user['password']) || empty('captcha')) {
                $this->showMessage('请填写完整信息', $this->createUrl('/user/resetpwd'));
            } else {
                $re = User::model()->retrievePassword($user);
                if ($re['errno'] == 0) {
                    $this->showSuccess('修改成功', $this->createUrl('/user/login'));
                } else {
                    $this->showMessage('失败' . $re['errmsg'], $this->createUrl('/user/resetpwd'));
                }
            }
        }
        $this->renderPartial('changepsw');
    }

}
